﻿using Autofac;
using Microsoft.Extensions.Configuration;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SD.App.Desktop
{
    public class ConfigurationModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            // 2. 加载 appsettings.json（低版本兼容：直接拼接绝对路径）
            IConfiguration configuration = LoadConfiguration();

            // ① 注册 IConfiguration 为单例（核心：后续通过 DI 注入使用）
            builder.RegisterInstance(configuration)
                   .As<IConfiguration>()
                   .SingleInstance();
        }

        /// <summary>
        /// 加载 appsettings.json 配置
        /// </summary>
        private IConfiguration LoadConfiguration()
        {
            var configuration = new ConfigurationBuilder()
            .SetBasePath(AppContext.BaseDirectory)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .Build();

            Log.Information("配置文件加载成功");

            return configuration;
        }
    }
}
